//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Shipments.h"
#include "FormUtilities.h"
#include "ShipmentDlg.h"


//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
__fastcall TfrmShipments::TfrmShipments(TComponent* Owner, TDatabase * dbMain)
        : TForm(Owner)
{
   TFormUtilities::LocateForm(( TForm*)Owner, this);

   this->dbMain = dbMain; 
   shipments = new TStringList();
   shipments->Duplicates = dupAccept;
   bridge = new TShipmentBridge( dbMain);
}

void TfrmShipments::SetButtonsState( int row)
{
   TShipment * shipment;
   bool enabled;

   enabled = ( shipments->Count > 0);
   
   btnUpdate->Enabled = enabled;
   btnDelete->Enabled = enabled;
}

void TfrmShipments::RefreshGrid()
{
   TShipment* shipment;
   int rowCount;
   int i;

   rowCount = 2;
   bridge->GetShipments( shipments);

   if ( shipments->Count > 1)
      rowCount = shipments->Count + 1;

   grdShipments->RowCount = rowCount;

   for ( i = 1; i < grdShipments->RowCount; i++)
      grdShipments->Rows[ i]->Clear();

   for ( i = 0; i < shipments->Count; i++)
   {
      shipment = ( TShipment*)shipments->Objects[ i];

      grdShipments->Cells[ 0][ i + 1] = IntToStr( shipment->number);
      grdShipments->Cells[ 1][ i + 1] = DateToStr( shipment->shipmentDate);
      AnsiString amount = shipment->amount;
      amount += " " + CurrencyString;
      grdShipments->Cells[ 2][ i + 1] = amount;
      grdShipments->Cells[ 3][ i + 1] = shipment->provider->name;
   }//for

   TFormUtilities::SetLastColumnWidth( grdShipments);

   SetButtonsState( grdShipments->Row);
}


//---------------------------------------------------------------------------

void __fastcall TfrmShipments::FormShow(TObject *Sender)
{
   grdShipments->Cells[ 0][ 0] = "SHIPMENT NO.";
   grdShipments->Cells[ 1][ 0] = "SHIPMENT DATE";
   grdShipments->Cells[ 2][ 0] = "AMOUNT";
   grdShipments->Cells[ 3][ 0] = "PROVIDER";

   RefreshGrid();
}
//---------------------------------------------------------------------------


void __fastcall TfrmShipments::btnAddClick(TObject *Sender)
{
   TfrmShipmentDlg * frmShipmentDlg;

   frmShipmentDlg = new TfrmShipmentDlg( this, dbMain, bridge, NULL);

   try
   {
      if ( frmShipmentDlg->ShowModal() == mrOk)
         RefreshGrid();
   }
   __finally
   {
      delete frmShipmentDlg;
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmShipments::FormDestroy(TObject *Sender)
{
   delete bridge;
      
   TStringUtilites :: Clear( shipments);
   delete shipments;
}
//---------------------------------------------------------------------------

void __fastcall TfrmShipments::btnDeleteClick(TObject *Sender)
{
   TShipment* shipment;

   shipment = ( TShipment*)shipments->Objects[ grdShipments->Row - 1];
   
   if ( MessageDlg( "Are you sure ?",
                    mtWarning,
                    TMsgDlgButtons() << mbYes << mbNo,
                    0) == mrYes)
   {
        bridge->Delete( shipment->number, shipment->shipmentDate);
        delete shipment;
        shipments->Delete( grdShipments->Row - 1);
        RefreshGrid();
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmShipments::grdShipmentsSelectCell(TObject *Sender,
      int ACol, int ARow, bool &CanSelect)
{
   SetButtonsState( ARow);        
}
//---------------------------------------------------------------------------

void __fastcall TfrmShipments::btnUpdateClick(TObject *Sender)
{
   TfrmShipmentDlg * frmShipmentDlg;
   TShipment * shipment;

   shipment = ( TShipment*)shipments->Objects[ grdShipments->Row - 1];
   frmShipmentDlg = new TfrmShipmentDlg( this, dbMain, bridge, shipment);

   try
   {
      if ( frmShipmentDlg->ShowModal() == mrOk)
         RefreshGrid();
   }
   __finally
   {
      delete frmShipmentDlg;
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmShipments::grdShipmentsKeyDown(TObject *Sender,
      WORD &Key, TShiftState Shift)
{
   switch ( Key)
   {
      case VK_INSERT :
         btnAddClick( Sender);
         break;

      case VK_DELETE :
         if ( btnDelete->Enabled)
            btnDeleteClick( Sender);
         break;

      case VK_SPACE :
         if ( btnUpdate->Enabled)
            btnUpdateClick( Sender);
         break;
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmShipments::btnCloseClick(TObject *Sender)
{
   Close();        
}
//---------------------------------------------------------------------------


